home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-23 | 10.5 KB | 152 lines | [TEXT/ALFA] |
-
- LESSON 0 "What Forth is and is not"
- --------------------------------------------------------------------------------
-
- Contents of this "lesson":
-
- 1. The Forth Paradigm
- 2. Forth in relation to other programming languages
- 3. Why use it?
-
- ================================================================================
-
- This "lesson" attempts (note the word "attempts") to give the reader a
- sense of what Forth is, what it is not, and why one would bother to
- learn and use it. The real lessons start in the next file.
-
-
-
- The Forth Paradigm: "What Forth is"
- -------------------------------------
-
- Forth was created to allow programmers the freedom to program as they
- see fit. It is built of four basic parts: the Dictionary, the Stack,
- the Return Stack, and the Interpreter/Compiler.
-
- When programming in Forth the key elements are the Dictionary and the
- Stack. The stack is the dynamic element of Forth, all activity takes
- place on it during program execution. Active things act on something,
- and the object and source of the stack activity in Forth is the
- dictionary. The dictionary is static and does not change during
- program execution (except for data written to reserved spaces) but is
- defined prior to execution. The dictionary contains all the program code
- and data. While there are variations in this arrangement, it is true in
- general:
-
-
- +---------------------+
- | |
- | |
- | | +---+
- | The | | | <- The Stack
- | | +---+ (data manipulation)
- | Dictionary | | |
- | | +---+
- | (the code) | | |
- | | +---+
- +---------------------+
-
-
- Typically, the programmer creates the program source code and tests it
- by loading it into the interpreter/compiler. This is often done word
- by word or screen by screen if using blocks. Once all the code is
- written and tested many Forths allow the programmer to create a
- standalone application (Macintosh terminology) by loading the code,
- selecting a startup word and sealing off the rest of the system from
- the user. This sealed system will appear as any other binary to the
- user.
-
- Again, a Forth program is built of words, which often use other words
- and sometimes make use of fixed data, all located within the
- dictionary. An application is created by defining a "highest level"
- word, the word that runs the entire program, like main() in C, or the
- outermost BEGIN .. END. in Pascal. Note that MacQForth cannot create
- standalone Macintosh applications but can save a memory image with a
- startup word specified so that it will run when loaded.
-
- Think of it this way. In order to solve a particular problem the
- programmer builds a dictionary. The dictionary contains all the words
- that are needed to express the solution to the problem, to speak the
- language of the problem. These words are put together to "speak" the
- solution. The advantage, of course, is that while every problem is
- different, many problems share some of the same words in their
- "spoken" solution. Therefore, Forth is highly reusable, contrary to
- the charge of some critics that it is not. Factoring, a Forth term
- that means removing discrete elements from a word that could be used
- elsewhere, makes this reusablility possible. What level of factoring
- is good is a matter of some debate.
-
- Many older Forths, and some newer ones too, go beyond the idea of
- programming language and become an entire programming
- language/operating system combination. It becomes a means for
- controlling the entire computer. Instead of a compiler or interpreter
- that runs programs created in a separate editor using files on disk
- mantained by an operating system, these Forths _are_ the compiler,
- _are_ the interpreter, _are_ the editor, and _are_ the operating
- system. There is no distinction, and no overhead. These are the
- block-based Forths and might be worth investigating after learning
- with MacQForth.
-
-
-
-
- Forth in relation to other programming languages: "What Forth is not"
- ----------------------------------------------------------------------
-
- Forth was meant as something more than a programming language. In a
- traditional block-based Forth this is true. It is somewhat less true
- in newer file-based Forths, however. Still, Forth in any form has
- much to offer that distinguishes it from other languages. Here are
- some comparisions:
-
- Forth is not an interpreter like BASIC, or Lisp, although it has an
- interpreter within it.
-
- Forth is not a fixed compiler like C or Pascal, although it uses a
- compiler.
-
- Forth is not an unstructured programming language like COBOL or older
- FORTRANs. (no GOTO, not even with a compiler switch :) , but one could
- be added if you as programmer feel it is necessary)
-
- Forth is not a low-level language like assembly, but it contains
- low-level instructions and often contains an assembler.
-
- If one were forced to choose, Forth is somewhat like C, but far freer
- than C. As one humor piece put it:
-
- The task: Shoot yourself in the foot.
- ---------------------------------------
- C: "You shoot yourself in the foot."
- Forth: "Foot yourself shoot in." (remember: Forth is postfix)
-
-
- I think of Forth and C in this way:
-
- +---------------------------+
- | |
- | Forth +---------------+ | Forth can do what C does and
- | | | | anything else that C doesn't
- | | C | | do by extending the compiler.
- | | | |
- | | | |
- | +---------------+ |
- | |
- +---------------------------+
-
- (Note: Forth cannot do what C++ does :)
-
- While it is at about the same level as C, i.e. a mix of high-level
- and low-level, Forth has the advantages of a built in interpreter and a
- high level of extensibility. If the C compiler does not have what
- you want, too bad. If the Forth compiler does not have what you
- want, then simply extend the compiler with CREATE and DOES>, or even
- alter the nucleus (or kernel) and metacompile the system (metacompile
- is a Forth term for rebuilding the system. Most Forths are written
- largely in Forth and can compile themselves.)
-
- In some views, Forth is less a programming language than it is a
- system for creating custom programming languages to suit the
- application at hand.
-
- Many conside